import os
import sys

class RsyncWrapper(object):
	def __init__(self, rsync_cmd_tpl):
		self.rsync_cmd_tpl = rsync_cmd_tpl

	def _rsync(self, f_path):
		cmd = self.rsync_cmd_tpl.replace("$SYNC_FILE", f_path)
		print cmd
		rs = os.system(cmd)
		return rs == 0
		# return True

	def list_files(self, dir_name):
		def call(arg, dirname, fnames):
			for f in fnames:
				f_path = os.path.join(dirname, f)
				if not os.path.exists(f_path):
					continue
				elif os.path.isdir(f_path):
					os.path.walk(f_path, call, None)
				else:
					for i in xrange(100):
						print "start rsync %s"%(f_path)
						if self._rsync(f_path):
							break
						print "rsync %s faild count %d"%(f_path, i)
		call(None, "", dir_name)


def main():
	if len(sys.argv) < 3:
		print "usage: %s up_file"%(sys.argv[0])
		sys.exit(1)
	target_fs = sys.argv[1]
	source_fs = sys.argv[2:]
	rsync_cmd = r'rsync.cmd -Pr --rsh="ssh.exe -i D:\keys\nmy_ssh_key" $SOURCE $TARGET'
	# rw = RsyncWrapper("rsync $SYNC_FILE")
	rw = RsyncWrapper(rsync_cmd)
	rw.list_files(source_fs)

if __name__ == "__main__":
	main()